package com.samsung.spen.sshare.main.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SMSSender {

	final String SENT = "SMS_SENT";
    final String DELIVERED = "SMS_DELIVERED";
    
	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(SENT)){
				switch (getResultCode()) {
					 case Activity.RESULT_OK:
                         Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                         Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_NO_SERVICE:
                         Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_NULL_PDU:
                         Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_RADIO_OFF:
                         Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
                         break;
				}
			}else if(intent.getAction().equals(DELIVERED)){
				switch (getResultCode())
	            {
	                case Activity.RESULT_OK:
	                    Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
	                    break;
	                case Activity.RESULT_CANCELED:
	                    Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
	                    break;
	            }
			}
			
			context.unregisterReceiver(broadcastReceiver);
		}
    	
    };
    
	public void sendSMS(Context context, String phoneNumber, String content) {
        
        
        
        context.registerReceiver(broadcastReceiver, new IntentFilter(SENT));
        context.registerReceiver(broadcastReceiver, new IntentFilter(DELIVERED));
        
		SmsManager smsManager = SmsManager.getDefault();
		ArrayList<String> contentParts = smsManager.divideMessage(content);
		ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
		ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
		for(int i = 0; i<contentParts.size(); i++){
			PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);
			sentIntents.add(sentPI);
	        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);
	        deliveryIntents.add(deliveredPI);
		}
		
		smsManager.sendMultipartTextMessage(phoneNumber, null, contentParts, sentIntents, deliveryIntents);
	}
}


